﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;
using WHC.Security.Entity;

namespace WHC.Hotel.UI.Report
{
    public partial class TeaSigningReport : BaseForm
    {
        private decimal cash = 0M;//现金
        private decimal card = 0M;//储值卡
        private decimal daijin = 0M;//代金劵
        private decimal national = 0M;//国内信用卡
        private decimal interCredit = 0M;//国际信用卡
        private decimal deposit = 0M;//账面押金

        public TeaSigningReport()
        {
            InitializeComponent();
            InitDictItem();
        }

        private void ResetZero()
        {
            cash = 0M;//现金
            card = 0M;//储值卡
            daijin = 0M;//代金劵
            national = 0M;//国内信用卡
            interCredit = 0M;//国际信用卡
            deposit = 0M;//账面押金
        }

        private void InitDictItem()
        {
            this.txtPayType.Items.Clear();
            this.txtPayType.Items.AddRange(DictItemUtil.GetPayType());
            if (this.txtPayType.Items.Count > 0)
            {
                this.txtPayType.SelectedIndex = 0;
            }

            this.txtCreator.Items.Clear();
            List<UserInfo> userList = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetAll();
            foreach (UserInfo info in userList)
            {
                this.txtCreator.Items.Add(info.FullName);
            }
            this.txtCreator.Items.Insert(0, "所有收银人");
        }

        private void KFCheckoutReport_Load(object sender, EventArgs e)
        {
            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;
            this.winGridView1.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

            this.winGridView2.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView2.AppendedMenu = this.contextMenuStrip2;
            this.winGridView1.dataGridView1.RowEnter += new DataGridViewCellEventHandler(dataGridView1_RowEnter);
            #region 添加别名解析
            //RoomNo,ItemName,Price,Discount,DiscountPrice,Quantity,Amount,BeginTime,EndTime,Waiter,Creator
            this.winGridView2.AddColumnAlias("RoomNo", "餐台号");
            this.winGridView2.AddColumnAlias("ItemName", "项目名称");
            this.winGridView2.AddColumnAlias("Price", "单价");
            this.winGridView2.AddColumnAlias("Discount", "打折比例");
            this.winGridView2.AddColumnAlias("DiscountPrice", "折后单价");
            this.winGridView2.AddColumnAlias("Quantity", "数量");
            this.winGridView2.AddColumnAlias("Amount", "应收金额");
            this.winGridView2.AddColumnAlias("BeginTime", "消费开始时间");
            this.winGridView2.AddColumnAlias("EndTime", "消费结束时间");
            this.winGridView2.AddColumnAlias("Waiter", "服务生");
            this.winGridView2.AddColumnAlias("Creator", "记账人");

            #endregion

            this.dtStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row;
            for (int i = 0; i < winGridView1.dataGridView1.SelectedRows.Count; i++)
            {
                row = winGridView1.dataGridView1.SelectedRows[i];
                string billNumber = row.Cells["BillNumber"].Value.ToString();
                if (!string.IsNullOrEmpty(billNumber))
                {
                    this.winGridView2.PrintTitle = Portal.gc.gAppUnit + " -- " + string.Format("账单[{0}]的消费明细列表", billNumber);
                    this.winGridView2.DisplayColumns = "RoomNo,ItemName,Price,Discount,DiscountPrice,Quantity,Amount,BeginTime,EndTime,Waiter,Creator";
                    
                    List<TeaConsumerListInfo> list = BLLFactory<TeaOrder>.Instance.GetBillDetailReport(billNumber);
                    this.winGridView2.DataSource = new WHC.Pager.WinControl.SortableBindingList<TeaConsumerListInfo>(list);
                }

                break;
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //ResetZero();
            //DataGridViewRow row;
            //for (int i = 0; i < winGridView1.dataGridView1.Rows.Count; i++)
            //{
            //    row = winGridView1.dataGridView1.Rows[i];
            //    cash += Convert.ToDecimal(row.Cells["Cash"].Value);
            //    card += Convert.ToDecimal(row.Cells["Card"].Value);
            //    daijin += Convert.ToDecimal(row.Cells["DaijinTicket"].Value);
            //    national += Convert.ToDecimal(row.Cells["NationalCredit"].Value);
            //    interCredit += Convert.ToDecimal(row.Cells["InterCredit"].Value);
            //    deposit += Convert.ToDecimal(row.Cells["Deposit"].Value);
            //}

            //this.lblCash.Text = cash.ToString("C2");
            //this.lblCard.Text = card.ToString("C2");
            //this.lblDaijin.Text = daijin.ToString("C2");
            //this.lblDeposit.Text = deposit.ToString("C2");
            //this.lblNational.Text = national.ToString("C2");
            //this.lblInterCredit.Text = interCredit.ToString("C2");
        }

        private void BindData()
        {
            //BillNumber, RoomNo, CustomerName, Deposit, Consumption, OfferMoney, PayMoney, KeepChange, Creator, 
            //PayType, Cash,Card,DaijinTicket, NationalCredit,InterCredit, case Invoice when '1' then '是' else '否' end as Invoice,
            //CreateDate,InHotelDate,CheckStatus,(CreditCardNumber+ InterCreditCardNumber) CreditCard,Note

            //this.lblCreator.Text = this.txtCreator.Text;

            this.winGridView1.DisplayColumns = "BillNumber,RoomNo,CustomerName,Deposit,Consumption,PayMoney,Difference,CreateDate,CheckStatus,Invoice,Note";

            #region 添加别名解析
            this.winGridView1.AddColumnAlias("BillNumber", "结账单号");
            this.winGridView1.AddColumnAlias("RoomNo", "餐台号");
            this.winGridView1.AddColumnAlias("CustomerName", "签单客户");
            this.winGridView1.AddColumnAlias("Deposit", "押金");
            this.winGridView1.AddColumnAlias("Consumption", "消费金额");
            this.winGridView1.AddColumnAlias("OfferMoney", "优惠金额");
            this.winGridView1.AddColumnAlias("PayMoney", "实收金额");
            this.winGridView1.AddColumnAlias("Difference", "支付差额");
            this.winGridView1.AddColumnAlias("KeepChange", "找零");
            this.winGridView1.AddColumnAlias("Creator", "收款人");
            this.winGridView1.AddColumnAlias("PayType", "收款方式");
            this.winGridView1.AddColumnAlias("Cash", "现金");
            this.winGridView1.AddColumnAlias("Card", "储值卡");
            this.winGridView1.AddColumnAlias("DaijinTicket", "代金劵");
            this.winGridView1.AddColumnAlias("NationalCredit", "国内信用卡");
            this.winGridView1.AddColumnAlias("InterCredit", "国际信用卡");
            this.winGridView1.AddColumnAlias("Invoice", "是否开发票");
            this.winGridView1.AddColumnAlias("CreateDate", "收款时间");
            this.winGridView1.AddColumnAlias("InHotelDate", "进店时间");
            this.winGridView1.AddColumnAlias("CheckStatus", "结账状态");
            this.winGridView1.AddColumnAlias("CreditCard", "信用卡号");
            this.winGridView1.AddColumnAlias("Note", "结账备注");
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("CreateDate", this.dtStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
                .AddCondition("CreateDate", this.dtEnd.Value.ToString(), SqlOperator.LessThanOrEqual)
                .AddCondition("PayType", this.txtPayType.Text, SqlOperator.Like)
                .AddCondition("CustomerName", this.txtName.Text, SqlOperator.Like)
                .AddCondition("CheckStatus", "签单", SqlOperator.Equal);
            if (string.IsNullOrEmpty(this.txtCreator.Text) || this.txtCreator.Text == "所有收银人")
            {
                //this.lblCreator.Text = "所有收银人";
            }
            else
            {
                condition.AddCondition("Creator", this.txtCreator.Text, SqlOperator.Like);
            }
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);

            DataTable dt = BLLFactory<TeaSuiteBill>.Instance.GetSigningReport(filter);
            #region 增加汇总信息
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                decimal Consumption = 0M;
                decimal PayMoney = 0M;
                decimal Difference = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    Consumption += Convert.ToDecimal(row["Consumption"]);
                    PayMoney += Convert.ToDecimal(row["PayMoney"]);
                    Difference += Convert.ToDecimal(row["Difference"] ?? 0M);
                }
                dr["BillNumber"] = "合计：";
                dr["Consumption"] = Consumption;
                dr["PayMoney"] = PayMoney;
                dr["Difference"] = Difference;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            }
            #endregion

            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "茶室签单信息查询报表";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void menu_InRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void txtPayType_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindData();
            }
        }

        private void txtCreator_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindData();
            }
        }

        private void menu_DetailRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void menu_ModifyStatus_Click(object sender, EventArgs e)
        {
            DataGridViewRow row;
            for (int i = 0; i < winGridView1.dataGridView1.SelectedRows.Count; i++)
            {
                row = winGridView1.dataGridView1.SelectedRows[i];
                string billNumber = row.Cells["BillNumber"].Value.ToString();
                if (!string.IsNullOrEmpty(billNumber))
                {
                    TeaSuiteBillInfo info = BLLFactory<TeaSuiteBill>.Instance.FindByBillNumber(billNumber);
                    if (info != null)
                    {
                        FrmEditSignCheckStatus dlg = new FrmEditSignCheckStatus();
                        dlg.lblBillNumber.Text = info.BillNumber;
                        dlg.lblCheckStatus.Text = info.CheckStatus;
                        dlg.lblRoomNo.Text = info.RoomNo;
                        dlg.lblRoomNoDesc.Text = "餐台号";
                        dlg.txtAmount.Text = info.PayMoney.ToString("f2");
                        dlg.chkFP.Checked = info.Invoice;
                        dlg.txtReceivedMoney.Text = info.ReceivedMoney.ToString("f2");

                        if (dlg.ShowDialog() == DialogResult.OK)
                        {
                            info.CheckStatus = dlg.txtStatus.Text;
                            info.Invoice = dlg.chkFP.Checked;
                            info.PayMoney = Convert.ToDecimal(dlg.txtAmount.Text);
                            info.ReceivedMoney = Convert.ToDecimal(dlg.txtReceivedMoney.Text);
                            BLLFactory<TeaSuiteBill>.Instance.Update(info, info.ID.ToString());

                            BindData();
                        }
                    }
                }
            }
        }

        private void txtCustomerName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void ChangeCustomer(string customerID)
        {
            if (!string.IsNullOrEmpty(customerID))
            {
                CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(customerID);
                if (info != null)
                {
                    this.txtName.Text = info.Name;
                }
            }
        }

        private void btnNameSearch_Click(object sender, EventArgs e)
        {
            FrmCustomerSearch dlg = new FrmCustomerSearch();
            dlg.txtName.Text = this.txtName.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(dlg.SelectedID))
                {
                    ChangeCustomer(dlg.SelectedID);
                }
            }
        }

    }
}
